home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / datapipe.c < prev    next >
C/C++ Source or Header  |  1998-07-17  |  4KB  |  175 lines

  1. /* 
  2.  
  3. The following is an interesting snippet of code I wrote recently. It 
  4. makes a data pipe between a listen port on the machine it's being run on 
  5. and a port on a remote machine. For example, running
  6.   datapipe 2222 23 your.machine.com
  7.  
  8. would create a port 2222 on the local machine that, if telnetted to, would
  9. be the same as telnetting to port 23 on your.machine.com. This can be used
  10. for a variety of purposes: redirect IRC connections so that identd shows
  11. the username of the datapipe process; redirect sendmail direct connections
  12. for the same reason; even use on a firewall machine to give access to an
  13. internal service (ftpd, for instance). Cascaded datapipes make for
  14. interesting traceback dilemmas. Questions and comments accepted.
  15.  
  16. Compile with:
  17.     cc -o datapipe -O datapipe.c
  18. On boxes without strerror() (like SunOS 4.x), compile with:
  19.     cc -o datapipe -O -DSTRERROR datapipe.c
  20.  
  21. Run as:
  22.     datapipe localport remoteport remotehost
  23.  
  24. It will fork itself into the background.
  25.  
  26. /*
  27.  * Datapipe - Create a listen socket to pipe connections to another
  28.  * machine/port. 'localport' accepts connections on the machine running    
  29.  * datapipe, which will connect to 'remoteport' on 'remotehost'. Fairly
  30.  * standard 500 xxxx extended errors are used if something drastic
  31.  * happens.
  32.  *
  33.  * (c) 1995 Todd Vierling
  34.  *
  35.  * Define STRERROR while compiling on a SunOS 4.x box
  36.  */
  37.  
  38. #include <sys/types.h>
  39. #include <sys/socket.h>
  40. #include <sys/wait.h>
  41. #include <netinet/in.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <errno.h>
  45. #include <unistd.h>
  46. #include <netdb.h>
  47.  
  48. #include <linux/time.h>
  49.  
  50. #ifdef STRERROR
  51. extern char *sys_errlist[];
  52. extern int sys_nerr;
  53. char *undef = "Undefined error";
  54.  
  55. char *strerror(error)  
  56.   int error;  
  57.   if (error > sys_nerr)
  58.     return undef;
  59.   return sys_errlist[error];
  60. }
  61. #endif
  62.  
  63. main(argc, argv)  
  64.   int argc;  
  65.   char **argv;  
  66.   int lsock, csock, osock;
  67.   FILE *cfile;
  68.   char buf[4096];
  69.   struct sockaddr_in laddr, caddr, oaddr;
  70.   int caddrlen = sizeof(caddr);
  71.   fd_set fdsr, fdse;
  72.   struct hostent *h;
  73.   struct servent *s;
  74.   int nbyt;
  75.   unsigned long a;
  76.   unsigned short oport;
  77.  
  78.   if (argc != 4) {
  79.     fprintf(stderr,"Usage: %s localport remoteport remotehost\n",argv[0]);
  80.     return 30;
  81.   }
  82.   a = inet_addr(argv[3]);
  83.   if (!(h = gethostbyname(argv[3])) &&
  84.       !(h = gethostbyaddr(&a, 4, AF_INET))) {
  85.     perror(argv[3]);
  86.     return 25;
  87.   }
  88.   oport = atol(argv[2]);
  89.   laddr.sin_port = htons((unsigned short)(atol(argv[1])));
  90.   if ((lsock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
  91.     perror("socket");
  92.     return 20;
  93.   }
  94.   laddr.sin_family = htons(AF_INET);
  95.   laddr.sin_addr.s_addr = htonl(0);
  96.   if (bind(lsock, &laddr, sizeof(laddr))) {
  97.     perror("bind");
  98.     return 20;
  99.   }
  100.   if (listen(lsock, 1)) {
  101.     perror("listen");
  102.     return 20;
  103.   }
  104.   if ((nbyt = fork()) == -1) {
  105.     perror("fork");
  106.     return 20;
  107.   }
  108.   if (nbyt > 0)
  109.     return 0;
  110.   setsid();
  111.   while ((csock = accept(lsock, &caddr, &caddrlen)) != -1) {
  112.     cfile = fdopen(csock,"r+");
  113.     if ((nbyt = fork()) == -1) {
  114.       fprintf(cfile, "500 fork: %s\n", strerror(errno));
  115.       shutdown(csock,2);
  116.       fclose(cfile);
  117.       continue;
  118.     }
  119.     if (nbyt == 0)
  120.       goto gotsock;
  121.     fclose(cfile);
  122.     while (waitpid(-1, NULL, WNOHANG) > 0);
  123.   }
  124.   return 20;
  125.  
  126.  gotsock:
  127.   if ((osock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
  128.     fprintf(cfile, "500 socket: %s\n", strerror(errno));
  129.     goto quit1;
  130.   }
  131.   oaddr.sin_family = h->h_addrtype;
  132.   oaddr.sin_port = htons(oport);
  133.   memcpy(&oaddr.sin_addr, h->h_addr, h->h_length);
  134.   if (connect(osock, &oaddr, sizeof(oaddr))) {
  135.     fprintf(cfile, "500 connect: %s\n", strerror(errno));
  136.     goto quit1;
  137.   }
  138.   while (1) {
  139.     FD_ZERO(&fdsr);
  140.     FD_ZERO(&fdse);
  141.     FD_SET(csock,&fdsr);
  142.     FD_SET(csock,&fdse);
  143.     FD_SET(osock,&fdsr);
  144.     FD_SET(osock,&fdse);
  145.     if (select(20, &fdsr, NULL, &fdse, NULL) == -1) {
  146.       fprintf(cfile, "500 select: %s\n", strerror(errno));
  147.       goto quit2;
  148.     }
  149.     if (FD_ISSET(csock,&fdsr) || FD_ISSET(csock,&fdse)) {
  150.       if ((nbyt = read(csock,buf,4096)) <= 0)
  151.     goto quit2;
  152.       if ((write(osock,buf,nbyt)) <= 0)
  153.     goto quit2;
  154.     } else if (FD_ISSET(osock,&fdsr) || FD_ISSET(osock,&fdse)) {
  155.       if ((nbyt = read(osock,buf,4096)) <= 0)
  156.     goto quit2;
  157.       if ((write(csock,buf,nbyt)) <= 0)
  158.     goto quit2;
  159.     }
  160.   }
  161.  
  162.  quit2:
  163.   shutdown(osock,2);
  164.   close(osock);
  165.  quit1:
  166.   fflush(cfile);
  167.   shutdown(csock,2);
  168.  quit0:
  169.   fclose(cfile);
  170.   return 0;
  171. }
  172.  
  173.